<template>
  <div class="page-box">
    <el-card class="page-box-card">
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        label-width="88px"
        inline
      >
        <!-- <el-form-item label="类别" prop="param.type">
          <el-select
            style="width: 240px"
            v-model="queryParams.param.type"
            placeholder="请选择类别"
          >
            <el-option
              v-for="item in dict.type.strategy_type"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="规则名称" prop="param.name">
          <el-input
            v-model="queryParams.param.name"
            placeholder="请输入规则名称"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="small"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="small" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['definition:class:add']"
            >新增</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleEdit"
            v-hasPermi="['definition:class:edit']"
            >修改</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['definition:class:delete']"
            >删除</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            v-hasPermi="['definition:class:download']"
            @click="downloadData"
            >导出</el-button
          >
        </el-col>
      </el-row>
      <el-table
        v-loading="loading"
        :data="dataList"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" fixed />
        <el-table-column
          type="index"
          label="序号"
          width="55"
          align="center"
          fixed
        />
        <el-table-column prop="type" label="类别" align="center" width="100">
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.strategy_type"
              :value="scope.row.type"
            />
          </template>
        </el-table-column>
        <el-table-column
          prop="name"
          label="规则名称"
          align="center"
          width="180"
        >
        </el-table-column>
        <el-table-column
          prop="formula"
          label="计算公式"
          align="center"
          width="180"
        >
        </el-table-column>
        <el-table-column prop="content" label="规则描述"> </el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="160">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleEdit(scope.row)"
              v-hasPermi="['system:dept:edit']"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="text"
              style="color: red"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:dept:edit']"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
    <!-- 添加或修改对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="addDialog"
      width="800px"
      append-to-body
    >
      <el-form ref="form" :model="addForm" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="类别" prop="type">
              <el-select
                style="width: 100%"
                v-model="addForm.type"
                placeholder="请选择类别"
              >
                <el-option
                  v-for="item in dict.type.strategy_type"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value - 0"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="规则名称" prop="name">
              <el-input
                v-model="addForm.name"
                placeholder="请输入规则名称"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="分子变量" prop="molecule">
              <el-input
                v-model="addForm.molecule"
                placeholder="请输入分子变量"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="分母变量" prop="denominator">
              <el-input
                v-model="addForm.denominator"
                placeholder="请输入分母变量"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="规则计算公式" prop="formula">
              <el-input
                v-model="addForm.formula"
                placeholder="请输入规则计算公式"
              ></el-input>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="变量类别" prop="required">
              <el-radio-group v-model="addForm.required">
                <el-radio :label="1">是</el-radio>
                <el-radio :label="0">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col> -->
          <el-col :span="24">
            <el-form-item label="规则描述" prop="content">
              <el-input
                style="width: 100%"
                type="textarea"
                :rows="6"
                placeholder="请输入内容"
                v-model="addForm.content"
              >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  inserFlupStrategy,
  updateFlupStrategy,
  deleteFlupStrategy,
  exportData,
} from "@/api/followUp/strategy";
import { queryRule } from "@/api/quality/rules";
export default {
  dicts: ["strategy_type", "strategy_danger_level"],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      selectionData: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        param: {
          name: "",
        },
      },
      tagList: [],
      dataList: [],
      total: 0,
      title: "",
      addDialog: false,
      addForm: {
        type: "",
        riskLevel: "",
        roundList: [],
        round: "",
        content: "",
      },
      rules: {
        text: [{ required: true, message: "请输入变量名称", trigger: "blur" }],
        code: [{ required: true, message: "请输入变量CODE", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 导出数据
    downloadData() {
      const that = this;
      this.$modal
        .confirm("是否确认导出当前查询条件下的数据？")
        .then(function () {
          // 导出
          const params = that.queryParams;
          return that.download(
            exportData,
            { ...params.param },
            `策略_${new Date().getTime()}.xlsx`
          );
        })
        .then((res) => {
          that.$message.success("导出成功");
        })
        .catch(() => {});
    },
    // 搜索事件
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 重置事件
    resetQuery() {
      this.dataList = [];
      this.total = 0;
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 10;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 获取数据列表
    getList() {
      this.loading = true;
      const param = { ...this.queryParams };
      queryRule(param).then((response) => {
        this.dataList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 列表选中事件
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.selectionData = [...selection];
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    // 新增变量
    handleAdd() {
      this.title = "新增随访策略";
      this.addDialog = true;
    },
    // 新增/修改弹框提交事件
    async submitForm() {
      const param = { ...this.addForm };
      if (param.roundList.length) {
        param.round = param.roundList.join(",");
      } else {
        param.round = "";
      }
      if (param.id) {
        // 修改
        await updateFlupStrategy(param).then((res) => {
          this.$message.success("修改成功");
          this.cancel();
          this.handleQuery();
        });
      } else {
        // 新增
        await inserFlupStrategy(param).then((res) => {
          this.$message.success("新增成功");
          this.cancel();
          this.handleQuery();
        });
      }
    },
    // 新增/修改弹框取消事件
    cancel() {
      this.addForm = {
        type: "",
        riskLevel: "",
        roundList: [],
        round: "",
        content: "",
      };
      this.addDialog = false;
    },
    // 修改变量
    handleEdit(row) {
      this.title = "修改策略";
      const data = row.id ? row : this.selectionData[0];
      this.addForm = {
        ...data,
        roundList: data.round.split(","),
      };
      this.addDialog = true;
    },
    // 删除策略
    handleDelete(row) {
      const idList = row.id ? [row.id] : [...this.ids];
      this.$modal
        .confirm("是否确认删除当前选择的策略？")
        .then(function () {
          // 删除
          return deleteFlupStrategy({ idList });
        })
        .then(() => {
          this.$message.success("删除成功");
          this.handleQuery();
        })
        .catch(() => {});
    },
  },
};
</script>
<style lang="scss">
.row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}
.row-1 {
  width: 20%;
  display: flex;
  align-items: center;
  justify-content: center;
}
.row-3 {
  width: 80%;
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>
